# -*- coding: utf-8 -*-

import json
import tkinter as tk
import tkinter.messagebox
import pyperclip

from data.data_out import get_source, save_data_labeled, update_source


class MAIN(object):

    def __init__(self, master, **kwargs):
        global data_str, country_str, director_str, audience_str, genres_str
        global country_entry, director_entry, audience_entry, genres_entry
        global country_tag, director_tag, audience_tag, genres_tag
        data_str= tk.StringVar()

        country_tag = tk.StringVar()
        director_tag = tk.StringVar()
        audience_tag = tk.StringVar()
        genres_tag = tk.StringVar()

        country_str = tk.StringVar()
        director_str = tk.StringVar()
        audience_str = tk.StringVar()
        genres_str = tk.StringVar()
        data_str.set(json.dumps(data_json[0]))

        country_tag.set(data_json[0]['country'])
        director_tag.set(data_json[0]['director'])
        audience_tag.set(data_json[0]['audience_rating'])
        genres_tag.set(' '.join(data_json[0]['genres'].split('|')))

        country_str.set('inform:country = ' + data_json[0]['country'])
        director_str.set('inform:director = ' + data_json[0]['director'])
        audience_str.set('inform:audience_rating = ' + data_json[0]['audience_rating'])
        genres_str.set('inform:genres = ' + data_json[0]['genres'])

        # initialize window frame
        window = tk.Frame(master)
        window.grid()

        row_num = 0
        # add a label for instruction
        instruction_label = tk.Label(window,
                                     text=instruction_str,
                                     bg='yellow',     # 背景颜色
                                     font=('Arial', 15),  # 字体和字体大小
                                     # width=16, height=2  # 标签长宽
                                     ).grid(row=row_num, sticky='w')
        row_num = row_num + 1
        agent_label1 = tk.Label(window,
                                text='Which country movie do you want?',
                                font=('Arial', 15),  # 字体
                                ).grid(row=row_num, column = 1, sticky='e')
        row_num = row_num + 1

        inform_label1 = tk.Label(window,
                                 textvariable=country_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                                 text='Tag:',
                                 font=('Arial', 15),  # 字体
                                 ).grid(row=row_num, column = 1,sticky='w')

        country_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=country_tag)
        country_tag_entry.grid(row=row_num, column=0,columnspan = 2,  sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                              text='User Write:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, sticky='w')

        country_entry = tk.Entry(window, width = 47, font = 14)
        country_entry.bind('<FocusIn>', lambda event:self.set_text(country_tag.get()))
        country_entry.grid(row=row_num, column=0,columnspan = 2,sticky = 'e')
        row_num = row_num + 1

        agent_label2 = tk.Label(window,
                                text='which director do you like?',  # 标签的文字
                                font=('Arial', 15),  # 字
                                #wraplength=600,
                                #anchor='e',
                                #justify='right'
                                ).grid(row=row_num, column = 1, sticky='e')
        row_num = row_num + 1

        inform_label2 = tk.Label(window,
                                 textvariable=director_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                              text='Tag:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, column=1, sticky='w')

        director_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=director_tag)
        director_tag_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                               text='User Write:',
                               font=('Arial', 15),  # 字体
                               ).grid(row=row_num, sticky='w')

        director_entry = tk.Entry(window, width = 47, font = 14)
        director_entry.bind('<FocusIn>', lambda event:self.set_text(director_tag.get()))
        director_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        agent_label3 = tk.Label(window,
                                text='What audience rating range do you like?',  # 标签的文字
                                font=('Arial', 15),  # 字
                                #wraplength=600,
                                #anchor='e',
                                #justify='right'
                                ).grid(row=row_num,column = 1, sticky='e')
        row_num = row_num + 1

        inform_label2 = tk.Label(window,
                                 textvariable=audience_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                              text='Tag:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, column=1, sticky='w')

        audience_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=audience_tag)
        audience_tag_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                               text='User Write:',
                               font=('Arial', 15),  # 字体
                               ).grid(row=row_num, sticky='w')

        audience_entry = tk.Entry(window, width = 47, font = 14)
        audience_entry.bind('<FocusIn>', lambda event:self.set_text(audience_tag.get()))
        audience_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        agent_label4 = tk.Label(window,
                                text='Which genre do you like?',  # 标签的文字
                                font=('Arial', 15),  # 字
                                # width=15, height=2  # 标签长宽
                                #wraplength=600,
                                #anchor='e',
                                #justify='right'
                                ).grid(row=row_num, column = 1,sticky='e')
        row_num = row_num + 1

        inform_label3 = tk.Label(window,
                                 textvariable=genres_str,
                                 font=('Arial', 15),  # 字体
                                 #wraplength=600,
                                 #anchor='w',
                                 #justify='left'
                                 ).grid(row=row_num, sticky='w')

        tag_label1 = tk.Label(window,
                              text='Tag:',
                              font=('Arial', 15),  # 字体
                              ).grid(row=row_num, column=1, sticky='w')

        genres_tag_entry = tk.Entry(window, width = 21, font = 14, textvariable=genres_tag)
        genres_tag_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        user_label1 = tk.Label(window,
                               text='User Write:',
                               font=('Arial', 15),  # 字体
                               ).grid(row=row_num, sticky='w')

        genres_entry = tk.Entry(window, width = 47, font = 14)
        genres_entry.bind('<FocusIn>', lambda event:self.set_text(genres_tag.get()))
        genres_entry.grid(row=row_num, column=0, columnspan=2, sticky='e')
        row_num = row_num + 1

        supply_button = tk.Button(window, bg='yellow',text='提交', command = self.refresh_clicked)
        supply_button.grid(row=row_num, columnspan=2)

    def set_text(self, aString):  # 写入剪切板
        # w.OpenClipboard()
        # w.EmptyClipboard()
        # w.SetClipboardData(win32con.CF_TEXT, aString)
        # w.CloseClipboard()
        pyperclip.copy(aString)

    def refresh_clicked(event):
        global data_str, country_str, director_str, audience_str, genres_str
        global country_entry, director_entry, audience_entry, genres_entry
        global country_tag, director_tag, audience_tag, genres_tag

        data_labeled= []
        tag_labeled = []
        country_sentence = country_entry.get()
        director_sentence = director_entry.get()
        audience_sentence = audience_entry.get()
        genres_sentence = genres_entry.get()
        data_labeled.append(json.dumps(data_json[0]))
        data_labeled.append(country_sentence)
        data_labeled.append(director_sentence)
        data_labeled.append(audience_sentence)
        data_labeled.append(genres_sentence)

        if country_tag.get() in country_sentence:
            tag_labeled.append(country_sentence.replace(country_tag.get(), '<country>'))
        else:
            show_me()
            return
        if director_tag.get() in director_sentence:
            tag_labeled.append(director_sentence.replace(director_tag.get(), '<director>'))
        else:
            show_me()
            return
        if audience_tag.get() in audience_sentence:
            tag_labeled.append(audience_sentence.replace(audience_tag.get(), '<audience>'))
        else:
            show_me()
            return
        # if genres_tag.get() in genres_sentence:
        tag_labeled.append(genres_sentence.replace(genres_tag.get(), '<genres>'))
        # else:
        #     show_me()
        #     return

        # 删除输入框所有输入
        country_entry.delete(0, tk.END)
        director_entry.delete(0, tk.END)
        audience_entry.delete(0, tk.END)
        genres_entry.delete(0, tk.END)

        # 保存标注数据
        save_data_labeled(data_labeled)
        save_data_labeled(tag_labeled)

        data_json.pop(0)
        # 更新源数据
        update_source(data_json)

        data_str.set(data_json[0])
        country_str.set('inform:country = ' + data_json[0]['country'])
        director_str.set('inform:director = ' + data_json[0]['director'])
        audience_str.set('inform:audience = ' + data_json[0]['audience_rating'])
        genres_str.set('inform:genres = ' + data_json[0]['genres'])

        country_tag.set(data_json[0]['country'])
        director_tag.set(data_json[0]['director'])
        audience_tag.set(data_json[0]['audience_rating'])
        genres_tag.set(data_json[0]['genres'])


def show_me():
    tk.messagebox.showinfo(title='Hi', message='请确认您的输入中是否包含TAG')


data_json = get_source()
instruction_str = 'Please try to use different expressions'
# root = tk.Tk()
# app = MAIN(root)
# root.mainloop()